import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'

const request = axios.create({
	timeout: 5000
})

request.interceptors.request.use((config) => {
	config.headers!.token = localStorage.getItem('token') || ''
	return config
}, (error) => {
	return Promise.reject(error)
})


request.interceptors.response.use(response => {
	const { code } = response.data

	switch (code) {
		case 403:
			console.log('权限不够');
			break
		case 500:
			console.log('服务器有问题');
			break
		case 501:
			console.log('服务器有问题');
			break
		case 404:
			console.log('资源不存在');
			break
	}

	return response
}, (error) => {
	return Promise.reject(error)
})


export default request
